<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="占坑人">
              <j-search-select-user v-model="queryParam.createBy" placeholder="请输入并选择占坑人" :multiple="false" ></j-search-select-user>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="产品名称">
              <a-input placeholder="请输入产品名称" v-model.trim="queryParam.name"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="产品类目" :labelCol="{xs: { span: 24 },sm: { span: 7 },}" :wrapperCol="{xs: { span: 24 },sm: { span: 16 },}">
              <a-tree-select
                allow-clear
                v-model="queryParam.categoryId"
                :replaceFields="replaceFields"
                :filterTreeNode="filterTreeNode"
                show-search
                :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
                :tree-data="treeData"
                placeholder="请选择产品类目"
                tree-default-expand-all
                :getPopupContainer="node => node.parentNode"
              >
              </a-tree-select>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="占坑SKU">
                <a-input placeholder="请输入占坑SKU" v-model.trim="queryParam.code"></a-input>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <!-- <a-button type="primary" icon="download" @click="handleExportXls('产品库-标签')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload> -->
      <!-- 高级查询区域 -->
      <!-- <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query> -->
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchMoveToDevelopment"><a-icon type="delete"/>移至开发中</a-menu-item>
          <a-menu-item key="2" @click="batchDel_"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange"
        :customRow="selectRowData">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" style="width:80px;height: 80px;border-radius: 4px;object-fit: cover;cursor: pointer;" @click="handleImg(text)"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>
        <template slot="nameSlot" slot-scope="text">
          <j-ellipsis :value="text" :length="40" :limitLines="2"/>
        </template>
        <template slot="categoryFullNameSlot" slot-scope="text">
          <j-ellipsis :value="text" :length="40" :limitLines="2"/>
        </template>
        <template slot="createBySlot" slot-scope="text">
          <j-ellipsis :value="text" :length="20"/>
        </template>
        <template slot="remarkSlot" slot-scope="text">
          <j-ellipsis :value="text" :length="40" :limitLines="2"/>
        </template>

        <span slot="action" slot-scope="text, record">
          <!-- <a @click="handleEdit(record)">编辑</a> -->
          <a @click="handleMoveToDevelopment(record.id)" v-if="isShowDelete(record, 'develop')">移至开发中</a> 
          <a-divider type="vertical" v-if="isShowDelete(record, 'develop')" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <!-- 仅搜索状态为未下架与已完成的产品 -->
              <a-menu-item v-if="record.shelfStatus === '1' || record.preemptStatus === '7'">
                <a  @click="handleSearchImg(record)">查重搜图</a>
              </a-menu-item>
              <a-menu-item v-if="isShowDelete(record)">
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>

    <product-library-placeholder-modal ref="modalForm" @ok="modalFormOk"></product-library-placeholder-modal>
  </a-card>
</template>

<script>
  import Vue from 'vue'
  import { USER_INFO } from '@/store/mutation-types'
  import { postFormAction } from '@/api/manage'
  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import ProductLibraryPlaceholderModal from './modules/ProductLibraryPlaceholderModal'
  import {filterMultiDictText} from '@/components/dict/JDictSelectUtil'
  import { productMixins } from '@/views/productLibrary/mixins/productMixins'
  export default {
    name: 'ProductLibraryPlaceholderList',
    mixins:[JeecgListMixin, mixinDevice, productMixins],
    components: {
      ProductLibraryPlaceholderModal
    },
    data () {
      return {
        description: '产品库-标签管理页面',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'产品图片',
            align:"center",
            dataIndex: 'image',
            scopedSlots: { customRender: 'imgSlot' }
          },
          {
            title:'占坑SKU',
            align:"center",
            dataIndex: 'code'
          },
          {
            title:'产品名称',
            align:"center",
            dataIndex: 'name',
            scopedSlots: { customRender: 'nameSlot' }
          },
          {
            title:'产品类目',
            align:"center",
            dataIndex: 'categoryFullName',
            scopedSlots: { customRender: 'categoryFullNameSlot' }
          },
          {
            title:'占坑人',
            align:"center",
            dataIndex: 'createBy',
            scopedSlots: { customRender: 'createBySlot' }
          },
          {
            title:'占坑时间',
            align:"center",
            dataIndex: 'createTime'
          },
          {
            title:'下架倒计时',
            align:"center",
            dataIndex: 'offShelfCountdown'
          },
          {
            title:'状态',
            align:"center",
            dataIndex: 'shelfStatus_dictText'
          },
          {
            title:'是否完成',
            align:"center",
            dataIndex: 'completionStatus_dictText'
          },
          {
            title:'备注',
            align:"center",
            dataIndex: 'remark',
            scopedSlots: { customRender: 'remarkSlot' }
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/hzkj-product/product/productLibrary/preempt/list",
          delete: "/hzkj-product/product/productLibrary/preempt/delete",
          deleteBatch: "/hzkj-product/product/productLibrary/preempt/deleteBatch",
          exportXlsUrl: "/hzkj-product/product/productLibrary/preempt/exportXls",
          importExcelUrl: "hzkj-product/product/productLibrary/preempt/importExcel",
          moveToDevelopment: "/hzkj-product/product/productLibrary/preempt/moveToDevelopment"
        },
        dictOptions:{},
        superFieldList:[],
      }
    },
    created() {
      this.getSuperFieldList();
      this.loadData_('', 'queryMyCategory');
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    provide(){
      return {
        getProjectList: () => this.treeData
      }
    },
    methods: {
      initDictConfig(){
      },
      selectRowData(record,index){
        const that = this
        return {
          on:{
            dblclick:() => {
              // this.handleDetail(record)
            }
          }
        }
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'name',text:'标签名称',dictCode:''})
        fieldList.push({type:'int',value:'type',text:'标签类型',dictCode:'product_tag_type'})
        fieldList.push({type:'string',value:'createBy',text:'创建人',dictCode:''})
        this.superFieldList = fieldList
      },
      handleImg(text){
        if(text){
          this.$viewerApi({images: [text]});
        }
      },
      handleMoveToDevelopment(id){
        postFormAction(this.url.moveToDevelopment, { id }).then((res) => {
          if(res.success){
            this.$message.success(res.message);
            this.loadData();
            this.onClearSelected();
          }else{
            this.$message.warning(res.message);
          }
        })
      },
      isShowDelete(record, type){
        if(type === 'develop'){
          // 未下架
          return Vue.ls.get(USER_INFO).username === record.createBy && record.preemptStatus != '2' && record.shelfStatus != 2;
        }else{
          // 占坑人,移至审批中的产品不可删除
          return Vue.ls.get(USER_INFO).username === record.createBy && record.preemptStatus != '2';
        }
      },
      onSelectChange(selectedRowKeys, selectionRows) {
        // 批量删除自己占坑的产品，移至开发中的产品不可删除
        let flag = selectionRows.some(item => {
          return item.createBy != Vue.ls.get(USER_INFO).username;
        })
        if(flag){
          this.$message.warning('只可操作自己占坑的产品！');
          selectionRows = selectionRows.filter(item => item.createBy === Vue.ls.get(USER_INFO).username);
          return;
        }
        let flag_ = selectionRows.some(item => {
          return item.preemptStatus === 2;
        })
        if(flag_){
          this.$message.warning('开发中的产品不可进行操作！');
          selectionRows = selectionRows.filter(item => item.preemptStatus != 2);
          return;
        }
        this.selectedRowKeys = selectionRows.map(item => item.id);
        this.selectionRows = selectionRows;
      },
      batchMoveToDevelopment(){
        let flag = this.selectionRows.some(item => {
          return item.shelfStatus === 2;
        })
        let flag_ = this.selectionRows.some(item => {
          return item.preemptStatus === '2';
        })
        if(flag || flag_){
          if(flag){
            this.$message.warning('已下架的产品不可移至开发中！');
          }
          if(flag_){
            this.$message.warning('开发中的产品不可移至开发中！');
          }
          this.selectionRows = this.selectionRows.filter(item => !(item.shelfStatus === 2 || item.preemptStatus === '2'));
          this.selectedRowKeys = this.selectionRows.map(item => item.id);
          return;
        }
        this.handleMoveToDevelopment(this.selectedRowKeys.join(','));
      },
      handleSearchImg(record){
        let productStatus = '';
        if(record.preemptStatus === '7'){
          productStatus = '1';
        }
        if(record.shelfStatus === '2'){
          productStatus = '2';
        }
        this.$router.push({
          path:'/productLibrary/ProductLibraryImgList',
          query:{ params: encodeURIComponent(JSON.stringify({
            imageUrl: record.image,
            categoryId: record.categoryId,
            productStatus
          })) }
        })   
      },
      batchDel_(){
        let flag = this.selectionRows.some(item => {
          return item.preemptStatus === '2';
        })
        if(flag){
          this.$message.warning('开发中的产品不可删除！');
          this.selectionRows = this.selectionRows.filter(item => item.preemptStatus != '2');
          this.selectedRowKeys = this.selectionRows.map(item => item.id);
          return;
        }
        this.batchDel();
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>